<template>
	<div>
		<!-- 搜索框 -->
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="68px">
			<el-form-item label="字典名称" prop="dicType">
				<el-select v-model="queryParams.dicType">
					<el-option v-for="item in typeOptions" :key="item.dicTypeId" :label="item.dicTypeName"
						:value="item.dicType" />
				</el-select>
			</el-form-item>
			<el-form-item label="字典标签" prop="dicDataLabel">
				<el-input v-model="queryParams.dicDataLabel" placeholder="请输入字典标签" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="状态" prop="dicDataState">
				<el-select v-model="queryParams.dicDataState" placeholder="数据状态" clearable>
					<el-option v-for="dict in dict" :key="dict.value" :label="dict.label" :value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>
		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="addFrom()">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
					@click="updateData()">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="deleteData()">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-close" size="mini" @click="signOut()">返回</el-button>
			</el-col>
		</el-row>

		<template>
			<el-table ref="multipleTable" :data="dataList" tooltip-effect="dark" style="width: 100%"
				:header-cell-style="tableHeaderColor" @selection-change="handleSelectionChange">
				<el-table-column type="selection" width="55">
				</el-table-column>
				<el-table-column prop="dicDataId" label="字典编号" align="center">
				</el-table-column>


				<el-table-column prop="dicDataLabel" label="字典标签" align="center">
					<template slot-scope="scope">
						<span v-if="scope.row.echoStyle == '' 
						|| scope.row.echoStyle == 'default'">
							{{scope.row.dicDataLabel}}</span>
						<el-tag v-else :type="scope.row.echoStyle
					     ">{{scope.row.dicDataLabel}}</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="dicDataVal" label="字典键值" align="center" show-overflow-tooltip>
				</el-table-column>
				<el-table-column prop="showOrder" label="字典排序" align="center" show-overflow-tooltip>
				</el-table-column>

				<!-- <el-table-column
					  prop="dicDataState"
					  label="状态"
					  align="center"
					  show-overflow-tooltip>
					</el-table-column> -->
				<el-table-column prop="dicDataState" label="状态" align="center">
					<template v-slot="scope">
						<div v-if="scope.row.dicDataState == 0">
							<el-tag>正常</el-tag>
						</div>
						<div v-if="scope.row.dicDataState == 1">
							<el-tag type="info">停用</el-tag>
						</div>
					</template>
				</el-table-column>
				<el-table-column prop="remark" label="备注" align="center" show-overflow-tooltip>
				</el-table-column>
				<el-table-column label="创建日期" align="center">
					<template slot-scope="scope">{{ scope.row.createTime }}</template>
				</el-table-column>

				<el-table-column label="操作" align="center">
					<!-- <template slot-scope="scope">
					         <el-button
					           size="mini"
					           @click="handleEdit(scope.$index, scope.row)">修改</el-button>
					         <el-button
					           size="mini"
					           type="danger"
					           @click="handleDelete(scope.$index, scope.row)">删除</el-button>
					       </template>
					     </el-table-column> -->
					<template slot-scope="scope">
						<el-button size="mini" type="text" icon="el-icon-edit"
							@click="handleEdit(scope.$index,scope.row)" v-hasPermi="['system:dict:edit']">修改</el-button>
						<el-button size="mini" type="text" icon="el-icon-delete"
							@click="handleDelete(scope.$index,scope.row)" v-hasPermi="['system:dict:remove']">删除
</el-button>
					</template>
				</el-table-column>
			</el-table>

		</template>

		<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
			:current-page="queryParams.pageNum" :page-sizes="[3, 5, 8, 10]" :page-size="queryParams.pageSize"
			layout="->,total, sizes, prev, pager, next, jumper" :total="queryParams.total">
		</el-pagination>

		<!-- 添加或修改参数配置对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="字典类型">
					<el-input v-model="form.dicType" :disabled="true" />
				</el-form-item>
				<el-form-item label="数据标签" prop="dicDataLabel">
					<el-input v-model="form.dicDataLabel" placeholder="请输入数据标签" />
				</el-form-item>
				<el-form-item label="数据键值" prop="dicDataVal">
					<el-input v-model="form.dicDataVal" placeholder="请输入数据键值" />
				</el-form-item>
				<el-form-item label="样式属性" prop="styleProperty">
					<el-input v-model="form.styleProperty" placeholder="请输入样式属性" />
				</el-form-item>
				<el-form-item label="显示排序" prop="showOrder">
					<el-input-number v-model="form.showOrder" controls-position="right" :min="0" />
				</el-form-item>
				<el-form-item label="回显样式" prop="echoStyle">
					<el-select v-model="form.echoStyle">
						<el-option v-for="item in listClassOptions" :key="item.value"
							:label="item.label + '(' + item.value + ')'" :value="item.value"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="状态" prop="dicDataState">
					<el-radio-group v-model="form.dicDataState">
						<el-radio v-for="dict in dict" :key="dict.value" :label="dict.value">{{dict.label}}</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="备注" prop="remark">
					<el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>

	</div>
</template>

<script>
	export default {
		data() {
			return {
				queryParams: {
					dicType: '',
					dicDataLabel: '',
					dicDataState: '',
					pageNum: 1,
					pageSize: 3,
					total: 10,
				},
				typeOptions: [

				],
				dict: [{
						"label": "正常",
						"value": 0
					},
					{
						"label": "停用",
						"value": 1
					}
				],
				dataList: [],
				form: {
					dicDataId: '',
					dicType: '',
					dicDataLabel: '',
					dicDataVal: '',
					styleProperty: '',
					showOrder: '',
					echoStyle: '',
					dicDataState: 0,
					remark: '',
				},
				single: true,
				multiple: true,
				showSearch: true,
				chooseAll: [],
				open: false,
				listClassOptions: [{
						value: "default",
						label: "默认"
					},
					{
						value: "primary",
						label: "主要"
					},
					{
						value: "success",
						label: "成功"
					},
					{
						value: "info",
						label: "信息"
					},
					{
						value: "warning",
						label: "警告"
					},
					{
						value: "danger",
						label: "危险"
					}
				],
				rules: {
					dicDataLabel: [{
						required: true,
						message: "数据标签不能为空",
						trigger: "blur"
					}],
					dicDataVal: [{
						required: true,
						message: "数据键值不能为空",
						trigger: "blur"
					}],
					showOrder: [{
						required: true,
						message: "数据顺序不能为空",
						trigger: "blur"
					}]
				},
				title:'',
			};
		},
		methods: {
			//s设置表头行的样式
			tableHeaderColor({
				row,
				column,
				rowIndex,
				columnIndex
			}) {
				return 'background-color:#dcdcdc;color:#6b6b6b;font-wight:100;font-size:15px;text-align:center'

			},
			//退出点击函数
			signOut() {
				this.$router.push("/dic");
			},
			//表单删除(多删)
			deleteData() {
				let arr = this.chooseAll;
				let idArr = [];
				let i = 0;
				//拿到所有选中的数据从中获取所有id
				while (i < arr.length) {
					idArr.push(arr[i].dicDataId);
					i++;
				}
				let ids = idArr.toString();
				this.delData(ids);
			},
			//表单修改函数
			updateData() {
				this.title = "修改字典数据"
				this.open = true;
				console.log(this.chooseAll);
				this.form = this.chooseAll[0];
				this.form.dicType = this.queryParams.dicType;
			},
			//新增点击函数
			addFrom() {
				this.title = "新增字典数据"
				this.open = true;
				this.form = {
					dicDataId: '',
					dicType: '',
					dicDataLabel: '',
					dicDataVal: '',
					styleProperty: '',
					showOrder: '',
					echoStyle: '',
					dicDataState: 0,
					remark: '',
				};
				this.form.dicType = this.queryParams.dicType;

			},
			//表单取消
			cancel() {
				this.open = false;
				this.$refs["form"].resetFields();
			},
			//表单提交
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.dicDataId == '' || this.form.dicDataId == null || this.form.dicDataId ==
							undefined) {
							this.$axios.post("/data", this.form).then(resp => {
								if (resp.code == 20000) {
									this.$message.success(resp.msg);
									this.quarryAll(this.queryParams);
								} else {
									this.$message.error(resp.msg);
								}
							});

						} else {
							console.log(this.form);
							this.$axios.put("/data", this.form).then(resp => {
								if (resp.code == 20000) {
									this.$message.success(resp.msg);
									this.quarryAll(this.queryParams);
								} else {
									this.$message.error(resp.msg);
								}
							});
						}
						
						this.open = false;
					}
					
				});

				

			},
			//每页数量变化函数
			handleSizeChange(val) {
				console.log(`每页 ${val} 条`);
				this.queryParams.pageSize = val;
				this.quarryAll(this.queryParams);
			},
			//页数变化函数
			handleCurrentChange(val) {
				this.queryParams.pageNum = val;
				console.log(`当前页: ${val}`);
				this.quarryAll(this.queryParams);
			},
			//表单修改函数
			handleEdit(index, row) {
				this.title = "修改字典数据"
				this.open = true;
				let data = this.dataList[index];
				this.form = data;
				console.log(this.form);
				/* this.form.dicType = this.queryParams.dicType; */


			},
			//表单删除函数
			handleDelete(index, row) {
				let data = this.dataList[index];
				let id = data.dicDataId;
				this.delData(id);
			},
			//多选框函数
			handleSelectionChange(val) {
				this.chooseAll = val;
				console.log(this.chooseAll);
				if (val.length == 1) {
					this.single = false;
				} else {
					this.single = true;
				}
				if (val.length >= 1) {
					this.multiple = false;
				} else if (val.length == 0) {
					this.multiple = true;
				}

			},
			//搜索点击函数
			handleQuery() {
				this.quarryAll(this.queryParams);
			},
			//重置点击函数
			resetQuery() {
				this.queryParams.dicType = this.$route.query.dicType;
				this.queryParams.dicDataLabel = '';
				this.queryParams.dicDataState = '';
				this.quarryAll(this.queryParams);

			},
			quarryAll(params) {
				this.$axios.get("/data", {
					params: params
				}).then(resp => {
					if (resp.code == 20000) {
						console.log(resp);
						this.dataList = resp.data.list;
						this.queryParams.pageNum = resp.data.pageNum;
						this.queryParams.pageSize = resp.data.pageSize;
						this.queryParams.total = resp.data.total;
					}
				});
			},
			//删除函数
			delData(ids) {
				this.$confirm(`此操作将永久删除编号为${ids}的数据, 是否继续?`, '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$axios.delete("/data?ids=" + ids).then(resp => {
						if (resp.code == 20000) {
							this.$message.success(`删除编号为${ids}的数据` + resp.msg);
							this.quarryAll(this.queryParams);
						} else {
							this.$message.error(`删除编号为${ids}的数据` + resp.msg);
						}
					});
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除'
					});
				});
			}
		},
		mounted() {
			this.$axios.get("/type/allType").then(resp => {
				if (resp.code == 20000) {
					this.typeOptions = resp.data;
				}
			});
			console.log(this.$route.query);
			this.$axios.get("/data?dicType=" + this.$route.query.dicType).then(resp => {
				if (resp.code == 20000) {
					this.queryParams.dicType = this.$route.query.dicType;
					this.dataList = resp.data.list;
					this.queryParams.pageNum = resp.data.pageNum;
					this.queryParams.pageSize = resp.data.pageSize;
					this.queryParams.total = resp.data.total;
					console.log(this.dataList);
				}
			})
		}
	};
</script>

<style>
</style>
